package com.book.bookshop.config;

import com.book.bookshop.entity.BsUser;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
     // 开启拦截器
       HttpSession session = request.getSession();
       BsUser user = (BsUser) session.getAttribute("user");
       if (user != null && user.getUsername() != null){
           response.setHeader("has_login","true");
           return true;
       }else {
           response.setHeader("has_login","false");
           response.sendRedirect(request.getContextPath()+"/book/index");
           return false;
       }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
